<include file="Public:header"/>
<script type="text/javascript" src="__WEBPUBLIC__/Home/js/base.js"></script>
<div class="container">
	<div class="toolbar project clearfix" style="margin: 0">
		<span>{$project_info.p_name}<span style="color:#09c; text-decoration:underline; padding-left:10px">{$project_info.p_api_head}</span></span>
		<input type="hidden" name="p_id" value="{$project_info.p_id}" id="p_id" />
        <div style="float:right;padding-right: 50px;">
            <form action="{:U('Index/exportExcel')}" method="post">
                <!-- 选择字段 -->
                <div style="line-height: 30px;display: none;">
                    <input type="checkbox" value="项目名称||p_name" name="fields_data[]" checked/>&nbsp;项目名称　
                    <input type="checkbox" value="模块名称||mo_name" name="fields_data[]" checked/>&nbsp;模块名称　
                    <input type="checkbox" value="方法名称||f_zh_name" name="fields_data[]" checked/>&nbsp;方法名称　
                    <input type="checkbox" value="接口URL||f_url" name="fields_data[]" checked/>&nbsp;接口URL　
                    <input type="checkbox" value="传递方式||f_type" name="fields_data[]" checked/>&nbsp;传递方式　
                    <input type="checkbox" value="参数名称||pa_name" name="fields_data[]" checked/>&nbsp;参数名称　
                    <input type="checkbox" value="参数类型||p_type" name="fields_data[]" checked/>&nbsp;参数类型　
                    <input type="checkbox" value="能否为空||p_is_null" name="fields_data[]" checked/>&nbsp;能否为空　
                    <input type="checkbox" value="参数描述||p_desc" name="fields_data[]" checked/>&nbsp;参数描述　
                    <input type="checkbox" value="成功返回||f_success" name="fields_data[]" checked/>&nbsp;成功返回　
                    <input type="checkbox" value="失败返回||f_error" name="fields_data[]" checked/>&nbsp;失败返回　
                    <input type="hidden" name="p_id" value="{$project_info.p_id}" />
                </div>
                <button class="btn btn-primary" type="submit">导出该项目所有API</button>
            </form>
        </div>
	</div>
</div>
<div class="container" id="wrapper">
	<div class="bs-sidebar">
		<input type="hidden" id="mo-id" value="<php>echo $_GET['mo_id']</php>" />
		<input type="hidden" id="f-id" value="<php>echo $_GET['f_id']</php>" />
		<ul class="nav bs-sidenav" id="left-menu">
		<volist name="module_list" id="vo">
			<li class="mo-li" id="mo-li-{$vo.mo_id}">
			<if condition="$m_nickname">
				<span class="icon glyphicon glyphicon-plus" title="增加方法" data-id="{$vo.mo_id}" data-name="{$vo.mo_e_name}"></span>
				<span class="icon glyphicon glyphicon-remove remove-module" title="删除当前模块" data-id="{$vo.mo_id}"></span>
			</if>
				<a href="javascript:;" id="{$vo['mo_id']}" class="nav-top-item aa">{$vo.mo_name}</a>
				<ul class="nav">
					<volist name="vo['function_list']" id="voo">
						<li><a href="{:U('Function/index',array('p_id'=>$_GET['p_id'],'mo_id'=>$voo['mo_id'],'f_id'=>$voo['f_id']))}"
                            <eq name="_GET['f_id']" value="$voo['f_id']">style="color:red"</eq>>{$voo.f_zh_name}</a>
						<if condition="$m_nickname">
							<span class="icon glyphicon glyphicon-remove remove-fun" title="删除当前方法" data-id="{$voo.f_id}"></span>
						</if>
						</li>
					</volist>
				</ul>
			</li>
		</volist>
		</ul>
		<if condition="$m_nickname">
		<p id="add_text" class="hidden">
			<input type="text" name="mo_name" value="" id="mo_name" placeholder="模块名称" /><br />
			<input type="text" name="mo_e_name" value="" id="mo_e_name" placeholder="模块英文名称" style="margin-top:10px" />
		</p>
		<button type="button" class="btn btn-primary" id="addModuleShow">添加模块</button>
		</if>
	</div>
	
	<div class="content">
		<div class="content-text" id="content-text">
		<if condition="empty($function_info)">
			<h1>方法不存在或该方法已被删除！</h1>
		<else />
			<h1>{$function_info.f_zh_name}【/{$module_info.mo_e_name}/{$function_info.f_name}】</h1>
			<h2>方法描述：</h2>
			<p class="edit-icon">
				<if condition="$m_nickname"><span class="icon glyphicon glyphicon-pencil" title="编辑" data-name="f_desc"></span></if>
				<span class="c-txt">{$function_info.f_desc}</span>
			</p>
			<h2>接口调用请求说明：</h2>
			<p class="edit-icon">http请求方式：<span style="color:#09c">
				<if condition="$function_info.f_type eq 0">
				<php>if($m_nickname){echo '<select class="up-type" name="f_type"><option value="0" selected="selected">GET</option><option value="1">POST</option></select>';}else{echo 'GET';}
				</php>
				<else />
				<php>if($m_nickname){echo '<select class="up-type" name="f_type"><option value="0">GET</option><option value="1" selected="selected">POST</option></select>';}else{echo 'POST';}
				</php>
				</if>
				</span>
				<input type="hidden" value="" id="cur_type" />
			</p>
			<pre class="edit-icon"><if condition="$m_nickname"><span class="icon glyphicon glyphicon-pencil" title="编辑" data-name="f_url"></span></if><span class="c-txt">{$function_info.f_url}</span></pre>
			<if condition="empty($f_parameter)">
			<h2 class="pah2">参数说明：
			<p>没有参数</p>
			<else />
			<h2 class="pah2">参数说明：
			<php>if($m_nickname)echo '<span class="icon glyphicon glyphicon-pencil" title="编辑" data-name="f_pa"></span>';</php>
			</h2>
			<div id="pa_table">
				<table class="table table-bordered">
					<thead>
						<tr>
							<th width="15%">参数名称</th>
							<th width="55%">参数描述</th>
							<th width="10%">是否为空</th>
							<th width="10%">参数类型</th>
                            <th width="10%">示例值</th>
						</tr>
					</thead>
					<tbody>
					<volist name="f_parameter" id="vo">
						<tr>
							<td>{$vo.p_name}</td>
							<td>{$vo.p_desc}</td>
							<td><if condition="$vo['p_is_null'] eq 1">是<else />否</if></td>
							<td><if condition="$vo['p_type'] eq 0">文本<else />图片</if></td>
                            <td>{$vo.default_val|default="未提供"}</td>
						<tr>
					</volist>
					</tbody>
				</table>
			</div>
			</if>
			<h2>返回说明：</h2>
			<pre class="prettyprinted edit-icon"><if condition="$m_nickname"><span class="icon glyphicon glyphicon-pencil" title="编辑" data-name="f_success"></span></if><span class="c-txt">{$function_info.f_success}</span></pre>

            <h2>接口动态：</h2>
            <pre class="edit-icon">2015-6-25：“@熊永进”修改了【方法描述】【http请求方式】【请求URL】【参数说明】【返回说明】</pre>
        </if>
		</div>
		<div class="content-fixed hidden" id="content-fixed">
			<span class="close glyphicon glyphicon-remove " id="close"></span>
			<form action="{:U('Single/addFunction')}" method="post" class="form-horizontal" name="addFunction" role="form">
				<input type="hidden" name="mo_id" id="mo_id" data-id="" />
				<div class="form-group">
	                <label for="f_zh_name" class="col-sm-3 control-label"><em class="red">*</em>中文名称：</label>
	                <div class="col-sm-9">
	                  <input type="text" class="form-control must" id="f_zh_name" name="f_zh_name" placeholder="例如：用户注册">
	                </div>
	            </div>
	            <div class="form-group">
	                <label for="f_name" class="col-sm-3 control-label"><em class="red">*</em>方法名称：</label>
	                <div class="col-sm-9">
	                  <input type="text" class="form-control must" id="f_name" name="f_name" placeholder="例如：register">
	                </div>
	            </div>
	            <div class="form-group">
	                <label for="f_url" class="col-sm-3 control-label">请求URL：</label>
	                <div class="col-sm-9">
	                  <input type="text" class="form-control" id="f_url" name="f_url" value="{$project_info.p_api_head}" style="width:600px; position:relative">
	                </div>
	            </div>
	            <div class="form-group">
	                <label class="col-sm-3 control-label"><em class="red">*</em>请求类型：</label>
	                <div class="col-sm-9">
	                    <div class="checkbox">
                			<input type="radio" name="f_type" value="0" class="f_type f_type22" checked/>&nbsp;GET&nbsp;&nbsp;
                            <input type="radio" name="f_type" value="1" class="f_type f_type22" />&nbsp;POST
                		</div>
	                </div>
	            </div>
	            <div class="form-group">
	                <label class="col-sm-3 control-label">参数：</label>
	                <div class="col-sm-9" id="plus-input">
	                	<div class="checkbox" style="padding-bottom:8px;">
	                		<input type="radio" value="0" name="isPa" id="noPa" />&nbsp;无参&nbsp;&nbsp;
                            <input type="radio" name="isPa" id="yPa" value="1" checked/>&nbsp;有参
	                	</div>
	                    <p id="pas" style="width:600px; position:relative">
	                    	<select style="width:10%;height: 30px;" name="p_type[]" class="p_type">
	                    		<option value="0" title="参数类型">文本</option>
	                    		<option value="1" title="参数类型">图片</option>
	                    	</select>
	                    	<input type="text" name="p_name[]" class="p_name p_name2" placeholder="参数名称" style="width:35%"/>
	                    	<select style="width:10%;height: 30px;" name="p_is_null[]" class="p_is_null p_is_null2">
	                    		<option value="0" class="是否为空">否</option>
	                    		<option value="1" class="是否为空">是</option>
	                    	</select>
                            <input type="text" name="default_val[]" class="default_val default_val2" placeholder="示例值" style="width:35%"/>
	                    	<span class="icon glyphicon glyphicon-plus" id="add-input"></span><br/>
	                    	<textarea name="p_desc[]" placeholder="参数描述" class="p_desc" style="width:92%; margin-top:10px"></textarea>
	                    </p>
	                </div>
	            </div>
                <div class="form-group">
                    <label for="f_success" class="col-sm-3 control-label get_data" style="padding-left:0px;padding-top:0px;margin-left:31px;width:97px;height:30px;line-height: 30px;font-weight:normal; cursor: pointer;color:#fff;border-radius:4px;background-color:#357ebd;border-color:#357ebd;display:block;" title="点击获取返回数据" ><em class="red">*</em>返回数据：</label>
                    <div class="col-sm-9">
                        <textarea placeholder="当您正确填写接口和参数后点击【返回数据】将能自动触发请求返回数据。请务必整理返回数据并详细说明每个字段的含义。列表数据请只保留一个单元。" class="form-control must" id="f_success" name="f_success" style="width:700px; height:200px"></textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label for="f_desc" class="col-sm-3 control-label"><em class="red">*</em>方法描述：</label>
                    <div class="col-sm-9">
                        <textarea placeholder="如果该方法需要有强调和说明的地方请在此处进行详细说明" class="form-control must" id="f_desc" name="f_desc" style="width:700px;height:200px"></textarea>
                    </div>
                </div>
	            <!--<div class="form-group">
	                <label for="f_error" class="col-sm-3 control-label"><em class="red">*</em>失败返回：</label>
	                <div class="col-sm-9">
	                  <textarea type="text" class="form-control must" id="f_error" name="f_error" style="width:700px;height:200px"></textarea>
	                </div>
	            </div>-->
	            <div class="form-group">
	                <div class="col-sm-offset-3 col-sm-9">
	                  <button type="submit" class="btn btn-primary" id="addFun">确认添加</button>
	                </div>
	            </div>
		    </form>
		</div>
	</div>
</div>

<div id="editBox" class="box hidden">
	<h2 class="drag"></h2>
	<textarea data-id="" type="text" class="form-control" style=" height:200px; "></textarea>
	<button type="button" class="btn btn-primary" id="editFun">确认修改</button>
	<button class="close icon glyphicon glyphicon-remove"></button>
</div>

<!--修改参数-->
<div id="editPaBox" class="box hidden">
<h2 class="drag1"></h2>
<form action="{:U('Function/editPa')}" method="post" class="edit-pa-form">
	<span class="icon glyphicon glyphicon-plus add-input"></span>
	<div class="plus-pa">
		<volist name="f_parameter" id="vo">
		<p>
	    	<select style="width:10%" name="p_type[]" class="p_type">
	    		<option value="0" title="参数类型" <if condition="$vo['p_type'] eq 0">selected</if> >文本</option>
	    		<option value="1" title="参数类型" <if condition="$vo['p_type'] eq 1">selected</if>>图片</option>
	    	</select>
	    	<input type="text" value="{$vo.p_name}" name="p_name[]" class="p_name" placeholder="参数名称" style="width:35%"/>
	    	<select style="width:10%" name="p_is_null[]" class="p_is_null">
	    		<option value="0" class="是否为空" <if condition="$vo['p_is_null'] eq 0">selected</if>>否</option>
	    		<option value="1" class="是否为空" <if condition="$vo['p_is_null'] eq 1">selected</if>>是</option>
	    	</select>
            <if condition="$vo.default_val neq ''">
                <input type="text" name="default_val[]"  class="default_val" value="{$vo.default_val}" style="width: 35%"/>
                <else/>
                <input type="text" name="default_val[]"  class="default_val" value="" placeholder="请提供参数示例值" style="width: 35%"/>
            </if>
            <span class="icon glyphicon glyphicon-minus minus-input"></span><br/>
	    	<textarea type="text" name="p_desc[]" placeholder="参数描述" class="p_desc" style="width:100%; margin-top:10px">{$vo.p_desc}</textarea>
	    </p>
	    </volist>
	</div>
   	<input type="hidden" name="f_id" value="<php>echo $_GET['f_id']</php>" />
    <button type="submit" class="btn btn-primary" id="editFun">确认修改</button>
	<span class="close icon glyphicon glyphicon-remove"></span>
</form>
</div>
<!--修改参数 end-->

<script>
var addModule = "{:U('Single/addModule')}";
var addFunction = "{:U('Single/addFunction')}";
var delModule = "{:U('Single/deleteModule')}";
var delFunction = "{:U('Single/deleteFunction')}";
var linkUrl = "{:U('Function/index',array('p_id'=>$_GET['p_id']))}";
var pApiHead = "{$project_info.p_api_head}";//获取api链接头url
$(function(){
	//调用拖拽
	drag($('.drag'),$('#editBox'));
	drag($('.drag1'),$('#editPaBox'));
	$('#left-menu .aa').each(function(){
		$moid = $('#mo-id').val();
		$id = $(this).attr('id');
		if($id == $moid){
			$(this).addClass('active');
			$(this).next().show();
		}
	});

	//修改方法
	$('.edit-icon').find('.icon').click(function(){
		var $this = $(this);
		var id = $(this).attr('data-name');
		var editBox = $('#editBox');
		editBox.removeClass('hidden');
		editBox.find('.form-control').attr('data-id',id);
		$this.next().attr('id',id);
		center(500,157,editBox);
		//获取当前的内容
		var current_text = $this.next().text();
		editBox.find('.form-control').val(current_text);
		$('#editFun').click(function(){
			var fid = $('#f-id').val();
			var fnames = $(this).prev().attr('data-id');
			var vals = $(this).prev().val();
			$.ajax({
				url:"{:U('Function/editFunction')}",
				type:'POST',
				data:{fid:fid,vals:vals,fnames:fnames},
				success:function(data){
					$('#'+id).text(vals);
					editBox.addClass('hidden');
					$this.next().attr('id','');
                    alert(data.info);
				}
			});
		});
	});

	//修改请求类型
	$('.up-type').change(function(){
		var fid = $('#f-id').val();
		var fnames = 'f_type';
		var vals = $(this).val();
		$.ajax({
			url:"{:U('Function/editFunction')}",
			type:'POST',
			data:{fid:fid,vals:vals,fnames:fnames},
			success:function(data){
				alert(data.info);
			}
		});
	});

	//修改参数
	$('.pah2').find('.icon').click(function(){
		var $this = $(this);
		var id = $(this).attr('data-name');
		var editPaBox = $('#editPaBox');
		editPaBox.removeClass('hidden');
		center(500,257,editPaBox);
	});

	//修改参数时删除不需要的
	$('.plus-pa .glyphicon-minus').click(function(){
		$(this).parent().remove();
	});

	//关闭修改框
	$('#editBox .close').click(function(){
		$('#editBox').addClass('hidden');
	});
	$('#editPaBox .close').click(function(){
		$('#editPaBox').addClass('hidden');
	});
	
	$('#add-input').click(function(){
		var add = $(this).parents().find('#plus-input');
		createAddPa(add);
		$('#plus-input .glyphicon-minus').click(function(){
			var ps = $(this).parent();
			ps.remove();
		});
	});

	$('.edit-pa-form .add-input').click(function(){
		var add = $(this).siblings('.plus-pa');
		createAddPa(add);
		$('.edit-pa-form .glyphicon-minus').click(function(){
			var ps = $(this).parent();
			ps.remove();
		});
	});

	$('#noPa').click(function(){
		$('#pas').hide();
	});
	$('#yPa').click(function(){
		$('#pas').show();
	});
});

function createAddPa(add){
    var html = '<p style="width:600px; position:relative">';
        html+='<select style="width:10%;height: 30px;" name="p_type[]">';
        html+='<option value="0" title="参数类型">文本</option>';
        html+='<option value="1" title="参数类型">图片</option>';
        html+='</select> ';
        html+='<input type="text" class="p_name p_name2" name="p_name[]" placeholder="参数名称" style="width:35%"/> ';
        html+='<select style="width:10%;height: 30px;" name="p_is_null[]" class="p_is_null2"> ';
        html+='<option value="0" title="是否为空">否</option>';
        html+='<option value="1" title="是否为空">是</option>';
        html+='</select>';
        html+='&nbsp;<input type="text" name="default_val[]"  class="default_val default_val2" placeholder="示例值" style="width:35%"/> ';
        html+='<span class="icon glyphicon glyphicon-minus minus-input"></span>';
        html+='<textarea name="p_desc[]" style="width:92%; margin-top:10px" placeholder="参数描述"></textarea> ';
        html+='</p>';
    $(add).append(html);
}

$(".get_data").click(function(){
    $("#f_success").val("数据请求中，请稍后~~");
    var f_url = $('#f_url').val();
    var type = '';
    var zt = document.getElementsByClassName("f_type22");
    for(var i = 0;i < zt.length;i++){
        if(zt[i].checked){
            if(zt[i].value == 0){
                type +='get';
            }else{
                type +='post';
            }
        }
    }
    var paNameGroup = $('.p_name2');
    var paValGroup = $('.default_val2');
    var pIsNull2 = $('.p_is_null2');
    var request_str = '';
    $(paNameGroup).each(function(index, element){
        //request_str += element + '=' + paNameGroup.index;
        if($(pIsNull2[index]).val() == 0){
            if (index < ($(paNameGroup).length - 1)) {
                request_str += $(element).val() + '=' + $(paValGroup[index]).val() + '&';
            } else {
                request_str += $(element).val() + '=' + $(paValGroup[index]).val();
            }
        }

    });
    $(".send_data").html("通过" + type + "发送的请求数据" + request_str);
    $.ajax({
        url:"{:U('ApiTool/getInfo')}",
        type:'post',
        data:{url:f_url,type:type,paGroup:request_str},
        success:function(data){
            if(data.json_ch){
                $("#f_success").val(JSON.stringify(JSON.parse(data.json), null, "    "));
            }else{
                $("#f_success").val("接口返回异常，请仔细检查请求URL和提交参数~~");
            }

        }
    });
});
</script>
<include file="Public:footer"/>